package com.jkj.http.controller;


import cn.hutool.core.lang.Dict;
import cn.hutool.system.SystemUtil;
import cn.hutool.system.UserInfo;
import com.jkj.http.entity.TreeSelect;
import com.jkj.utils.ExcelUtils;
import com.jkj.utils.JsonUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class IndexController {

    //    @RequestMapping(value = {"/"}, method = RequestMethod.GET)
//    public ModelAndView index() {
//        ModelAndView view = new ModelAndView();
//        view.setViewName("index");
//        return view;
//    }
    @RequestMapping(value = {"/systemInfo"}, method = RequestMethod.GET)
    public String systemInfo() {
        Dict dict = Dict.create();
        //dict.put("当前项目路径", System.getProperty("user.dir"));
        dict.put("class目录绝对路径", ClassUtils.getDefaultClassLoader().getResource("").getPath());
//        UserInfo userInfo = SystemUtil.getUserInfo();
//        dict.put("Java Virtual Machine Specification信息", SystemUtil.getJvmSpecInfo());
//        dict.put("Java Virtual Machine Implementation信息", SystemUtil.getJvmInfo());
//        dict.put("Java Specification信息", SystemUtil.getJavaSpecInfo());
//        dict.put("Java Implementation信息", SystemUtil.getJavaInfo());
//        dict.put("Java运行时信息", SystemUtil.getJavaRuntimeInfo());
//        dict.put("系统信息", SystemUtil.getOsInfo());
//        dict.put("当前目录",userInfo.getCurrentDir());
//        dict.put("当前主机网络地址信息", SystemUtil.getHostInfo());
//        dict.put(" 运行时信息，包括内存总大小、已用大小、可用大小等", SystemUtil.getRuntimeInfo());
        return JsonUtils.toJSON(dict);
    }

    @RequestMapping(value = {"/ueditor"}, method = RequestMethod.GET)
    public ModelAndView ueditor() {
        ModelAndView view = new ModelAndView();
        view.setViewName("ueditor");
        return view;
    }

    @RequestMapping(value = {"/404"}, method = RequestMethod.GET)
    public ModelAndView page404() {
        ModelAndView view = new ModelAndView();
        view.setViewName("404");
        return view;
    }


    @RequestMapping(value = "/export")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<TreeSelect> list = new ArrayList<TreeSelect>();
        list.add(new TreeSelect("123", "456"));
        list.add(new TreeSelect("456", "789"));
        list.add(new TreeSelect("789", "101112"));
        list.add(new TreeSelect("135", "246"));
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("id", "ID");
        headMap.put("text", "TEXT");
        XSSFWorkbook wb = ExcelUtils.exportExcel(list, headMap, "名称", true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition", "attachment;filename=student.xlsx");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    }
}
